var _ = require('underscore');
var Highcharts = require('highcharts');
var menuConfig = require('./menus.js');
require('jquery.cookie');

/**
  框架UI及多语言配置
*/

var $ = require('jquery')
  , moment = require('moment')
moment.locale('zh-cn')
Highcharts.setOptions({
  lang:{
    weekdays:['星期日','星期一','星期二','星期三','星期四','星期五','星期六'],
    shortMonths: ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12'],
    months:['一月','二月','三月','四月','五月','六月','七月','八','九月','十月','十一月','十二月'],
    noData:'没有要显示的数据'
  }
});
$(function(){
  var $account ={
    elmt: $('.account'),
    open: function(){
      this.elmt.find('.dropdown-main').slideDown(50);
      this.elmt.data('isOpen',true);
      this.elmt.find('.account-info').addClass('active')
    },
    close: function(){
      this.elmt.find('.dropdown-main').slideUp(50);
      this.elmt.data('isOpen',false);
      this.elmt.find('.account-info').removeClass('active')
    }
  }

  var $order ={
    elmt: $('.order'),
    open: function(){
      this.elmt.find('.dropdown-main').slideDown(50);
      this.elmt.data('isOpen',true);
      this.elmt.find('.order-title').addClass('active')
    },
    close: function(){
      this.elmt.find('.dropdown-main').slideUp(50);
      this.elmt.data('isOpen',false);
      this.elmt.find('.order-title').removeClass('active')
    }
  }


  $('.account').click(function  () {
    if (!$account.elmt.data("isOpen")) {
      $account.open();
    }else{
      $account.close();
    }
  })

  $('.order').click(function  () {
    if (!$order.elmt.data("isOpen")) {
      $order.open();
    }else{
      $order.close();
    }
  })

  $(document).click(function(e){
    if (!($(e.target).closest('.account').get(0)==$account.elmt.get(0))){
      $account.close();
    }
    if (!($(e.target).closest('.order').get(0)==$order.elmt.get(0))){
      $order.close();
    }
  });

  //===== 菜单
  var menus = $.cookie('menus');

  if (typeof menus === 'undefined') {
    $.cookie('menus', JSON.stringify(menuConfig), {
      expires: 99999,
      path: '/'
    });
  }

  try {
    menus = JSON.parse(menus);
  } catch(e) {
    menus = menuConfig;
  }

  menus = _.extend({}, menuConfig, menus);
  //===== 菜单

  var $expanded = $('.menu-group .active').parents('.menu-group.expand');

  $expanded.find('ul').show();

  $('.menu-group strong').on('click', function () {
    var $this = $(this);

    $parent = $this.parent('.menu-group');
    $parent.find('ul').slideToggle();
    $parent.toggleClass('expand');
    // $parent.siblings('.menu-group.expand').toggleClass('expand').find('ul').slideToggle(); // #MOBILE-1704

        // Get id attribute.
    var id = $this.attr('id'),
        expanded = $expanded.hasClass('expand');

    if (expanded) {
      $expanded.removeClass('active');
    } else {
      $expanded.addClass('active');
    }

    if (typeof menus[id] !== 'undefined') {
      menus[id] = $parent.hasClass('expand'); // Update menu status.
    }

    // Update menu cookie.
    $.cookie('menus', JSON.stringify(menus), {
      expires: 99999,
      path: '/'
    });
  });

  if (/overview1/.test(location.href)) {
    $expanded.find('[href*=overview]').addClass('active');
  }

  $('.server-access').remove()
  $('.btn-sidebar a').click(function(){
    $('.sidebar.active').slideToggle();
  });
});
module.exports = $;